{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "842b9393",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import io\n",
    "import base64\n",
    "from typing import List, Dict\n",
    "from PIL import Image\n",
    "import torchvision.transforms as transforms\n",
    "import logging\n",
    "import kserve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f35f528",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "class Transformer(object):\n",
    "    \"\"\" A class object for the data handling activities of Image Classification\n",
    "    Task and returns a KServe compatible response.\n",
    "\n",
    "    Args:\n",
    "        kserve (class object): The Model class from the KServe\n",
    "        module is passed here.\n",
    "    \"\"\"\n",
    "    def __init__(self):\n",
    "        logging.basicConfig(level=kserve.constants.KSERVE_LOGLEVEL)\n",
    "\n",
    "        self.image_processing = transforms.Compose([\n",
    "            transforms.ToTensor(),\n",
    "            transforms.Normalize((0.1307,), (0.3081,))\n",
    "        ])\n",
    "\n",
    "    def preprocess(self, inputs: Dict) -> Dict:\n",
    "        \"\"\"Pre-process activity of the Image Input data.\n",
    "\n",
    "        Args:\n",
    "            inputs (Dict): KServe http request\n",
    "\n",
    "        Returns:\n",
    "            Dict: Returns the request input after converting it into a tensor\n",
    "        \"\"\"\n",
    "        return {'instances': [self.image_transform(instance) for instance in inputs['instances']]}\n",
    "\n",
    "    def postprocess(self, inputs: List) -> List:\n",
    "        \"\"\"Post process function of Torchserve on the KServe side is\n",
    "        written here.\n",
    "\n",
    "        Args:\n",
    "            inputs (List): The list of the inputs\n",
    "\n",
    "        Returns:\n",
    "            List: If a post process functionality is specified, it converts that into\n",
    "            a list.\n",
    "        \"\"\"\n",
    "        return inputs\n",
    "\n",
    "    def image_transform(self, instance):\n",
    "        \"\"\"converts the input image of Bytes Array into Tensor\n",
    "\n",
    "        Args:\n",
    "            instance (dict): The request input to make an inference\n",
    "            request for.\n",
    "\n",
    "        Returns:\n",
    "            list: Returns the data key's value and converts that into a list\n",
    "            after converting it into a tensor\n",
    "        \"\"\"\n",
    "        byte_array = base64.b64decode(instance[\"data\"])\n",
    "        image = Image.open(io.BytesIO(byte_array))\n",
    "        instance[\"data\"] = self.image_processing(image).tolist()\n",
    "        return instance"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
